package handlers;

import java.util.Random;

import main.MainMap;
import main.MainMenu;
import people.Character;
import people.Enemy;
import constants.Colors;

public class PlayerAttackHandler extends Colors {

	public static String type = "";
	
	public static void handle(Enemy mob, int attackType) {
		// Attack Types:
		// 0 = melee
		// 1 = ranged
		// 2 = magic
		int dmg = -1;
		Character c = Character.c;
		Random rand = new Random();
		c.initStats();
		switch (attackType) {
			case 0: 
				if ((rand.nextInt(mob.stats[3]) > c.stats[5])) {
					dmg = -1;
					type += ("You have missed\r\n");
				}
				else {
					dmg = c.stats[2];
					mob.health = mob.health - dmg;
					type += ("You have hit the enemy for " + dmg + " damage\r\n");
				}
				break;
			case 1: 
				if ((rand.nextInt(mob.stats[3]) > c.stats[5])) {
					dmg = -1;
					type += ("You have missed\r\n");
				}
				else if ((rand.nextInt(mob.stats[5]) < c.stats[3] * 2)) {
					dmg = 50000;
					mob.health = mob.health - dmg;
					type +=("You have shot the enemy in the head, instantly killing it\r\n");
					MainMenu.printInfo(type);
					type = "";
					return;
				}
				else {
					dmg = (c.stats[2] / 2) + (c.stats[3] / 6);
					mob.health = mob.health - dmg;
					type += ("You have hit the enemy for " + dmg + " damage\r\n");
				}
				break;
			case 2:
				int mana_cost = rand.nextInt(5);
				c.mana = c.mana - mana_cost;
				dmg = c.stats[4];
				mob.health = mob.health - dmg;
				type += ("You have imbued the power of mana against the enemy, dealing " + dmg + " damage\r\n");
				break;
		}
		if (mob.health < 1) {
			GainExperience.handle(c, mob.level * constants.GeneralConstants.EXP_RATE);
			storage.EnemyStorage.removeEnemy(mob);
			MainMap.mainMap.map[c.x][c.y].setBackground(PLAYER_COLOR);
			type += ("You have killed the enemy");
			type = "";
			MainMenu.printInfo(type);
			return;
		}
		else {
			type += ("The monster now has " + mob.health + "/" + mob.stats[0] + " hp");
			MainMenu.printInfo(type);
			int mob_attackType;
			if (mob.job == 1) {
				mob_attackType = 3;
			}
			if (mob.job == 4) {
				mob_attackType = 2;
			}
			else {
				mob_attackType = 1;
			}
			EnemyAttackHandler.handle(c, mob_attackType, mob);
			MainMenu.printInfo(type);
			type = "";
		}
	} 
}
